package com.wejoy.video.middle.station.admin.controller.system;

import com.wejoy.video.middle.station.admin.util.CurrentUserUtils;
import com.wejoy.video.middle.station.admin.controller.BaseController;
import com.wejoy.video.middle.station.data.annotation.RoleCheck;
import com.wejoy.video.middle.station.data.domain.PageResult;
import com.wejoy.video.middle.station.data.vo.RespJSON;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.domain.system.SysResource;
import com.wejoy.video.middle.station.data.service.system.ISysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ArrayList;

/**
 * 系统--权限表
 *
 * @author admin
 * @date 2021年10月19日 15:06:30
 */
@RestController
@RequestMapping("/system/sysResource")
public class SysResourceController extends BaseController {

    @Autowired
    private ISysResourceService sysResourceService;

    /**
     * 保存或更新
     *
     * @param sysResourceReq sysResourceReq
     * @return RespJSON
     */
    @PostMapping("/save")
    @RoleCheck(name = "sys_resource_edit,sys_resource_add")
    public RespJSON save(@RequestBody SysResource sysResourceReq, @SessionAttribute(CurrentUserUtils.SYSTEM_USER)SysUser loginSysUser) {
        sysResourceService.save(sysResourceReq, loginSysUser.getUserName());
        return RespJSON.success();
    }

    /**
     * 分页查询
     *
     * @param sysResourceReq sysResourceReq
     * @return RespJSON
     */
    @PostMapping("/getPageList")
    @RoleCheck(name = "sys_resource_list")
    public RespJSON getPageList(@RequestBody SysResource sysResourceReq) {
        int count = sysResourceService.getCount(sysResourceReq);
        List<SysResource> list = new ArrayList<>();
        if (count > 0) {
            list = sysResourceService.getPageList(sysResourceReq);
        }
        PageResult result = new PageResult(sysResourceReq.getPageNo(), sysResourceReq.getPageSize(), count, list);
        return RespJSON.success(result);
    }

    /**
     * 目录树分页查询
     *
     * @param sysResourceReq sysResourceReq
     * @return RespJSON
     */
    @PostMapping("/getPageTreeList")
    @RoleCheck(name = "sys_resource_list")
    public RespJSON getPageTreeList(@RequestBody SysResource sysResourceReq) {
        int count = sysResourceService.getCount(sysResourceReq);
        List<SysResource> list = new ArrayList<>();
        if (count > 0) {
            list = sysResourceService.getPageTreeList(sysResourceReq);
        }
        PageResult result = new PageResult(sysResourceReq.getPageNo(), sysResourceReq.getPageSize(), count, list);
        return RespJSON.success(result);
    }


    /**
     * 主键查询
     *
     * @param id id
     * @return RespJSON
     */
    @RequestMapping("/getOneById")
    public RespJSON getOneById(Long id) {
        return RespJSON.success(sysResourceService.getOneById(id));
    }


    /**
     *获取资源树
     *
     * @return RespJSON
     */
    @RequestMapping("/getResourceTree")
    public RespJSON getResourceTree() {
        return RespJSON.success(sysResourceService.getResourceTree());
    }
}
